home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 2705 < prev    next >
Encoding:
Text File  |  1996-08-06  |  2.5 KB  |  89 lines

  1. Path: newsfeed.direct.ca!usenet
  2. From: qjackson@direct.ca
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: overloaded operator question
  5. Date: Fri, 19 Jan 1996 06:22:23 GMT
  6. Organization: Parsepolis Software
  7. Message-ID: <4dnd83$i20@grid.direct.ca>
  8. References: <4dmin1$160@noc2.drexel.edu>
  9. Reply-To: qjackson@direct.ca
  10. NNTP-Posting-Host: 204.174.245.127
  11. X-Newsreader: Forte Free Agent 1.0.82
  12.  
  13. st918h5w@dunx1.ocs.drexel.edu (Jonathan Juniman) wrote:
  14.  
  15. >What is wrong with the following declaration:
  16.  
  17. >MATRIX.H
  18. >class Matrix
  19. >    {
  20. >    public:
  21. >    // several other functions here...
  22. >    Matrix Matrix::operator *(Matrix&, Matrix&);
  23. >    }
  24. >  
  25. >My compiler (Microsoft Visual C++ v1.5) complains that there are too
  26. >many arguments.  How can there be too many arguments?  * is a binary
  27. >operator!  My intention is to perform matrix multiplication by 
  28. >typing a = b * c, where a, b and c are objects of type Matrix.
  29.  
  30. Operator overloads only have two parameters when they are outside the
  31. class.  In the case of a = b * c, where b and c are both of the Matrix
  32. type, there would only be one parameter, as in:
  33.  
  34. shuString shuString::operator + (const char* pChar)
  35. {
  36.  
  37.     shuString holder = *this;
  38.     holder.appendString(pChar);
  39.     return holder;
  40.  
  41. }
  42.  
  43. In the above example from my shuString class, I have overloaded the +
  44. operator for:
  45.  
  46.     result = leftarg + rightarg;
  47.  
  48. where rightarg is shuString and leftarg is char*.  Notice the use of
  49. *this within the function -- it refers to leftarg.
  50.  
  51. There are two ways to implement overloaded operators: as "friend"
  52. functions -- that is, outside the class but declared as friends within
  53. the class interface, or as class functions, that is, within the class.
  54. Friend functions have two parameters, class functions have one.  Most
  55. classes I've seen use class functions to implement overloads where the
  56. LEFT arg is an object of class x, whereas they use friend functions to
  57. implement overloads when the RIGHT arg is of class x.
  58.  
  59. That said and done, your declaration ought to read:
  60.  
  61.  
  62.  
  63.     class Matrix
  64.     {
  65.     // Foo
  66.     Matrix Matrix::operator* (Matrix&);
  67.     // Bar
  68.     }
  69.  
  70.     and your function ought to read:
  71.  
  72.     Matrix Matrix::operator* (Matrix& rightMatrix)
  73.     {
  74.  
  75.         // You refer to leftMatrix as *this within this function
  76.  
  77.         // Some foo here...
  78.  
  79.         return resultingMatrix;
  80.  
  81.     }
  82.  
  83. --      \|/                              O      |
  84.        --+--    Parsepolis Software     -*-   --|-- Quinn Tyler Jackson
  85.         /|\         "Parse City"        /^\     |     (aka 'Jamshid')
  86. >-----------------------------------------------|   qjackson@direct.ca
  87.        Ask me about Laleh's Pattern Matcher...  |--------------------->
  88.  
  89.